package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@Api
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Override
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        PageVO<CourseBaseDTO> courseBaseDTO = courseBaseService.queryCourseList(params,model,companyId);
        return courseBaseDTO;
    }

    /**
     * 添加录播课程
     * @param courseBaseVO
     * @return
     */
    @Override
    @PostMapping("course")
    public CourseBaseDTO creatCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBase = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        CourseBaseDTO courseBaseDTO = courseBaseService.creatCourseBase(courseBase);

        return courseBaseDTO;
    }

    @GetMapping("course/{courseId}")
    @Override
    public CourseBaseDTO getCourseBaseById(Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return courseBaseDTO;
    }

    @PutMapping("course")
    @Override
    public CourseBaseDTO modifyCourseBase(CourseBaseVO vo) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(vo);
        CourseBaseDTO dto = courseBaseService.modifyCourseBase(courseBaseDTO);
        return dto;
    }

    @Override
    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    /**
     * 课程提交
     * @param courseBaseId
     */
    @GetMapping("commit/{courseBaseId}")
    @Override
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId, companyId);

    }

}
